//
//  EmptyErrorView.swift
//  bilibili-link
//
//  Created by melvyn on 2018/8/23.
//  Copyright © 2018 NEET. All rights reserved.
//

import UIKit

class DZNEmptyDataView: UIView {
    
    private var imageView: UIImageView!
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        initialize()
    }

    override func didMoveToWindow() {
        print("didMoveToWindow")
    }
    
    override func willMove(toWindow newWindow: UIWindow?) {
        super.willMove(toWindow: newWindow)
        
        guard newWindow != nil, !imageView.isAnimating else {
            return
        }
        
        imageView.startAnimating()
    }
    
    func initialize() {
        
        imageView = UIImageView.init(frame: .zero)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        
        var animationImages: [UIImage] = []
        for i in 1..<5 {
            animationImages.append(UIImage(named: "livebase_common_loading_error_\(i)_400x400_")!)
        }
        
        imageView.animationImages = animationImages
        imageView.animationDuration = 0.8
        imageView.animationRepeatCount = Int.max
        imageView.startAnimating()
        addSubview(imageView)
        
        NSLayoutConstraint.activate([
            imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
            imageView.centerYAnchor.constraint(equalTo: centerYAnchor),
            imageView.widthAnchor.constraint(equalToConstant: 230),
            imageView.heightAnchor.constraint(equalToConstant: 230)
            ])
    }
}
